<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Export;
class Mission extends Common
{

//任务发布
    public function Mission(){
        
        $list = db('task')->order('id desc')->select();

        foreach ($list as $key => $value) {
            if(!empty($value['task_picture_url'])){
                
                $list[$key]['img_array'] = explode(',',$value['task_picture_url']);
            }else{
                $list[$key]['img_array'] =[];
            }   
        }

        $this->assign('list',$list);

        return $this->fetch();
    }

//任务发布修改
    public function edit_mission(){

        $id = input('id');

        if(!empty($id)){

            $da = db('task')->where('id',$id)->find();

            $da['img_array'] = explode(',',$da['task_picture_url']);
            
            $this->assign('da',$da);
        }

        return $this->fetch();
    }

//任务发布修改
    public function doedit_mission(){

        $id = input('id');
        $task_type = input('task_type');
        $task_content = input('task_content');
        $task_copywriting = input('task_copywriting');
        $task_commission = input('task_commission');
        $time = input('time');
        $time = strtotime($time);
        $data['task_type'] = $task_type;
        $data['task_content'] = $task_content;
       /* if(empty($data['task_content']))return json(['msg'=>'内容不能为空']);*/
        $data['task_copywriting'] = $task_copywriting;
        $data['task_commission'] = $task_commission;
        $data['end_time'] = $time;

        $file = request()->file('task_picture_url');
        
            if($file)
            {
                foreach($file as $fl){

                    $info = $fl->move(ROOT_PATH . 'public/static/uploads');
                    if($info){
                        // 成功上传后 获取上传信息
                        $img_array[] =  'http://'.$_SERVER['HTTP_HOST'] . '/static/uploads/'.$info->getSaveName();
                        if(!empty($id)){
                            $oldimg = db('task')->where(['id'=>$id])->value('task_picture_url');
                            $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                         /*   if(file_exists($oldimgurl)){
                                unlink($oldimgurl);
                            }*/
                        }
                    }else{
                        // 上传失败获取错误信息
                        return json(['msg'=>'上传失败']);
                    }
                }

                $data['task_picture_url'] = implode(',',$img_array);

            }

            if(empty($id)){
                //添加
                $data['created_at'] = time();
                $res = db('task')->insert($data);
            }else{
                //修改
                $task = db('task')->where('id',$id)->find();

                $time = $task['end_time'];

                $res = db('task')->where(['id'=>$id])->update($data);
                
                //如果修改时间小于 过期时间 任务失效
                if(!empty($data['end_time'])&&$data['end_time']<$time){

                    $sta['status'] = 3;
                    db('task_record')->where('task_id',$id)->where('status!=2')->update($sta);

                }

                
            }
            
            if(empty($res))return json(['msg'=>'更新失败']);
            return json(['msg'=>'更新成功']);       
    }

//任务审核
    public function examine_mission(){

        $phone = input('phone');
        if($phone){

            $where = "u.phone like '%$phone%'";
            
    /*        $uid['uid'] = db('user_account')->where($where)->value('id');
            
            $this->assign('phone', $phone);*/

        }else{

            $where = '';
        }

        $list = db('income')->alias('i')->where('i.status=1')->field('i.*,u.account')->join('user_account u','i.uid=u.id','LEFT')->where($where)->order('id desc')->paginate(10)->each(function($item, $key){

            if(!empty($item['uid'])&&!empty($item['task_record_id'])){

                $item['img_array'] = db('upload_task_img')->field('task_img')->where('uid',$item['uid'])->where('task_record_id',$item['task_record_id'])->select();      
            }else{
                $item['img_array'] =[];
            }

            $task_id = db('task_record')->where('id',$item['task_record_id'])->value('task_id');

            $img_str = db('task')->where('id',$task_id)->value('task_picture_url');

            if(!empty($img_str)){
                $item['img2'] = explode(',',$img_str);
            }

           /* dump($item);die;*/
            return $item;
        });

        /*dump($list->toArray());die;
*/

/*        $list = db('income')->where($uid)->where('status=1')->order('id desc')->paginate(10)->each(function($item, $key){

            if(!empty($item['uid'])){

                $item['account'] = db('user_account')->where('id',$item['uid'])->value('account');      
            }else{
                $item['account'] =[];
            }

            if(!empty($item['uid'])&&!empty($item['task_record_id'])){

                $item['img_array'] = db('upload_task_img')->field('task_img')->where('uid',$item['uid'])->where('task_record_id',$item['task_record_id'])->select();      
            }else{
                $item['img_array'] =[];
            }

            $task_id = db('task_record')->where('id',$item['task_record_id'])->value('task_id');

            $img_str = db('task')->where('id',$task_id)->value('task_picture_url');

            if(!empty($img_str)){
                $item['img2'] = explode(',',$img_str);
            }

            return $item;
        });*/


        $this->assign('page', $list->render());

       /* $data = $list->toArray();*/


        
        $this->assign('list',$list);

        return $this->fetch();
    }



//任务审核记录
    public function examine_record(){

        $account = input('account');
        $where =[];

        if($account){

            $where_account = '%'.$account.'%';

            $uid['uid'] = db('user_account')->where('account','like',$where_account)->value('id');

            $this->assign('account', $account);

        }else{

            $uid = '';
        }

        $status = input("status");
        if($status != ''){
           
            $where['status']=$status;
            $pageParam['query']['status'] = $status;
          
        }
        $this->assign('status',$status);


        $list = db('income')->where($uid)->where($where)->where('status!=1')->order('id desc')->paginate(15)->each(function($item, $key){

            if(!empty($item['uid'])){

                $item['account'] = db('user_account')->where('id',$item['uid'])->value('account');      
            }else{
                $item['account'] =[];
            }

            if(!empty($item['uid'])&&!empty($item['task_record_id'])){

                $item['img_array'] = db('upload_task_img')->field('task_img')->where('uid',$item['uid'])->where('task_record_id',$item['task_record_id'])->select();      
            }else{
                $item['img_array'] =[];
            }

            $task_id = db('task_record')->where('id',$item['task_record_id'])->value('task_id');

            $img_str = db('task')->where('id',$task_id)->value('task_picture_url');

            if(!empty($img_str)){
                $item['img2'] = explode(',',$img_str);
            }

            return $item;
        });


        $this->assign('page', $list->render());

        $data = $list->toArray();
        
        $this->assign('list',$data['data']);

        return $this->fetch();
    }


    //删除任务
    public function delmission(){

        $id =input('id');

        $res = db('task')->delete($id);

        if(empty($res))return json(['msg'=>'更新失败']);
        return json(['msg'=>'更新成功']);  
    }

//任务审核
    public function edit_examine(){

        $id = input('id');

        if(!empty($id)){

            $da = db('income')->where('id',$id)->find();

            if(!empty($da['uid'])&&!empty($da['task_record_id'])){
    
                $da['img_array'] = db('upload_task_img')->field('task_img')->where('uid',$da['uid'])->where('task_record_id',$da['task_record_id'])->select();  
      
            }else{

                $da['img_array'] =[];
            }


            $task_record_id=$da['task_record_id'];

            $task_id = db('task_record')->where('id',$task_record_id)->value('task_id');

            if(!empty($da['uid'])){

                $da['account'] = db('user_account')->where('id',$da['uid'])->value('account');      
            }else{
                $da['account'] =[];
            }


            $img_str = db('task')->where('id',$task_id)->value('task_picture_url');

            if(!empty($img_str)){
                $da['img2'] = explode(',',$img_str);
            }

            $this->assign('da',$da);
        }

        /*dump($da);die;*/

        return $this->fetch();
    }

    //任务审核改
    public function doedit_examine(){

        $id = input('id');
        $money = input('money');

        $content = input('content');
        $task_record_id = input('task_record_id');
        $status = input('status');
        $recommend_id = input('recommend_id');

        $income = db('income')->where('id',$id)->find();

        if(!$income){
            return json(['msg'=>'数据不存在']);
        }


            $reason['reason'] = input('reason');
            $reason['status'] = $status;
            $task_record_id = $task_record_id;

            $reason['update_at'] = time();
          /*  return ['msg'=>$task_record_id];die;*/
            $re = db('task_record')->where('id',$task_record_id)->update($reason);

            if(!$re)return json(['msg'=>'更新失败']);
        
        $data['money'] = $money;
        $data['id'] = $id;
        $data['content'] = $content;
        $data['task_record_id'] = $task_record_id;
        
        $data['status'] = $status;


            if(empty($id)){
                //添加
                $data['created_at'] = time();
                $res = db('income')->insert($data);
            }else{
                //修改
                $data['update_at'] = time();
                // 启动事务
                Db::startTrans();
                try{
                    $ress = db('income')->where(['id'=>$id])->update($data);
                  

                    //查询推荐人 一级
                    $recommend_one = db('team')->where('uid',$income['uid'])->value('recommend_id');
                   /* var_dump($recommend_one);die;*/


                    //查询推荐人 二级
                    if($recommend_one){

                        $recommend_two = db('team')->where('uid',$recommend_one)->value('recommend_id');
                    }

                    //通过要插入佣金表
                    if($status==2){

                        if($recommend_one){

                            $level = db('user_account')->where('id',$recommend_one)->value('level');

                            $tc_type='';

                            if($level==1){

                                $tc_type =2;

                            }elseif($level==2){

                                $tc_type =8;   
                            }

                            $one_task = db('proportion')->where('tc_type',$tc_type)->value('tc_price');
                
                            //通过要插入佣金表
                            $commission['uid'] = $recommend_one;
                           
                            $commission['commission'] = ($money*$one_task/100);
            
                            $commission['type'] = 1;
                            $commission['created_at'] = time();
                            $rc = db('team2_commission')->insert($commission);
                        }
                       
                     
                        if(!empty($recommend_two)){

                            $level = db('user_account')->where('id',$recommend_two)->value('level');

                            $tc_type='';

                            if($level==1){

                                $tc_type =4;

                            }elseif($level==2){

                                $tc_type =10;   
                            }

                            $two_task = db('proportion')->where('tc_type',$tc_type)->value('tc_price');
                            //通过要插入佣金表
                            $commission['uid'] = $recommend_two;

                            $commission['commission'] = ($money*$two_task/100);
                            $commission['type'] = 1;
                            $commission['created_at'] = time();
                            db('team1_commission')->insert($commission);
                        }

                        $task_status =[];
                        $task_status['status'] =2;

                        //任务状态
                        $rc = db('task_record')->where('uid',$income['uid'])->where('id',$task_record_id)->update($task_status);
                   
                    }

/*
                    if($status==3){

                        db('income')->delete($id);
                    }*/
                    
                    // 提交事务
                    Db::commit();
                    return json(['msg'=>'更新成功']);   
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return json(['msg'=>'更新失败']);
                }
                
            }
     
    }

    //批量审核
    public function updateall(){

        $id_str = input('id');

        $status = input('status');
        /*$title= input('title/a');*/       
        /*if($id==NULL) $this->error('请勾选要更新的数据');*/
        if(empty($id_str)){
            return json(['msg'=>'请勾选要更新的数据']);
        }else{

            $id_array = explode(',',$id_str);

        } 

/*        var_dump($id_array);die;*/
        foreach ($id_array as $key => $value) {


            $income = db('income')->where('id',$value)->find();



            if(!$income){
                return json(['msg'=>'数据不存在']);
            }

            $reason['reason'] = '';
            $reason['status'] = input('status');

            $task_record_id = $income['task_record_id'];

            $reason['update_at'] = time();
            $re = db('task_record')->where('id',$task_record_id)->update($reason);


          /*  if(!$re)return json(['msg'=>'更新失败']);*/
            
            $data['money'] = $income['money'];
            $data['id'] = $value;
            $data['content'] = $income['content'];
            $data['task_record_id'] = $task_record_id;
        
            $data['status'] = input('status');

            if(empty($value)){
                //添加
                $data['created_at'] = time();
                $res = db('income')->insert($data);
            }else{
        
                //修改
                $data['update_at'] = time();
                // 启动事务
         /*       Db::startTrans();
                try{*/
                    $ress = db('income')->where(['id'=>$value])->update($data);
                  

                    //查询推荐人 一级
                    $recommend_one = db('team')->where('uid',$income['uid'])->value('recommend_id');


                    //查询推荐人 二级
                    if($recommend_one){

                        $recommend_two = db('team')->where('uid',$recommend_one)->value('recommend_id');
                    }


                    //通过要插入佣金表
                    if($status==2){

                        if(!empty($recommend_one)){

                            $level = db('user_account')->where('id',$recommend_two)->value('level');

                            $tc_type='';

                            if($level==1){

                                $tc_type =2;

                            }elseif($level==2){

                                $tc_type =8;   
                            }

                            $one_task = db('proportion')->where('tc_type',$tc_type)->value('tc_price');
                            //通过要插入佣金表
                            $commission['uid'] = $recommend_one;
                            /*$commission['recommend_id'] = $recommend_id;*/
                            $commission['commission'] = ($data['money']*$one_task/100);
                            $commission['type'] = 1;
                            $commission['created_at'] = time();
                            $rc = db('team2_commission')->insert($commission);
                        }
                       

                        if(!empty($recommend_two)){

                            $tc_type='';

                            if($level==1){

                                $tc_type =4;

                            }elseif($level==2){

                                $tc_type =10;   
                            }

                            $two_task = db('proportion')->where('tc_type',$tc_type)->value('tc_price');
                     /*       $two_task = db('proportion')->where('tc_type',4)->value('tc_price');*/
                            //通过要插入佣金表
                            $commission['uid'] = $recommend_two;
                            /*$commission['recommend_id'] = $recommend_id;*/
                            $commission['commission'] = ($data['money']*$two_task/100);
                            $commission['type'] = 1;
                            $commission['created_at'] = time();
                            db('team1_commission')->insert($commission);
                        }

                        $task_status =[];
                        $task_status['status'] =2;
                  /*      echo 333333333;die;*/
                        //任务状态
                        $rc = db('task_record')->where('uid',$income['uid'])->where('id',$task_record_id)->update($task_status);
                   
                    }


               /*     if($status==3){

                        db('income')->delete($id);
                    }*/
                    
                    // 提交事务
                /*    Db::commit();*/
                    
              /*  } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();*/
                    /*return json(['msg'=>'更新失败']);*/
               /* }*/
                
            }
        }
        return json(['msg'=>'更新成功']);   
        
    /*    for($i=0;$i<count($id);$i++){
            $data['id'] = (int)$id[$i];
            $data['title'] = $title[$i];            
            UserModel::update($data);
        }
        */

    }


    //删除任务
    public function delexamine(){

        $id =input('id');

        $task_record_id = db('income')->where('id',$id)->find();

        $user = db('user_account')->where('id',$task_record_id['uid'])->value(['day_num']);

        $day_num['day_num']=$user+1;

        db('user_account')->where('id',$task_record_id['uid'])->update($day_num);

        $re = db('task_record')->delete($task_record_id['task_record_id']);

        $rre = db('upload_task_img')->delete($task_record_id['task_record_id']);


        $res = db('income')->delete($id);

        if(empty($res))return json(['msg'=>'更新失败']);
            return json(['msg'=>'更新成功']);  
    }

    //导出审核记录
    public function mission_export(){

        

        $list = db('income')->where('status!=1')->order('id desc')->select();
        if(empty($list)){
            $this->error('数据为空');
        }

        foreach ($list as $key => $value) {

           if(!empty($value['uid'])){

                $list[$key]['account'] = db('user_account')->where('id',$value['uid'])->value('account');

                $level = db('user_account')->where('id',$value['uid'])->value('level');

                switch ($level) {
                    case '0':
                        $list[$key]['new_level'] = '实习成员';
                        break;
                    case '1':
                        $list[$key]['new_level'] = '普通会员';
                    break;
                    case '2':
                        $list[$key]['new_level'] = '高级会员';
                    break;

                    default:
                        $list[$key]['new_level'] ='';
                        break;
                }

           

            }else{
                $list['account'] =[];
            }


            switch ($value['status']) {
                case '2':
                    $list[$key]['new_status'] = '审核通过';
                break;
                case '3':
                    $list[$key]['new_status'] = '审核不通过';
                break;

                default:
                    $list[$key]['new_status'] ='';
                break;
            }

            $data[$key]['account'] = $list[$key]['account'];

            $data[$key]['new_level'] = $list[$key]['new_level'];

            $data[$key]['content'] = $value['content'];

            $data[$key]['money'] = $value['money'];

            $data[$key]['new_status'] = $list[$key]['new_status'];

        }

        $headArr = array("会员账号","会员等级","任务内容",'任务金额','审核状态');
        
 /*       dump($list);die;
*/
        $data2 =array_filter($data);
        
         $filename='任务审核记录-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data2);
    } 


}
